package com.ipharma.gui;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.ipharma.McdError;
import com.ipharma.dto.Product;

/**
 * A Swing-based panel class.
 * <P>
 * @author JorgE.H
 */
public class GUIBarCode extends JPanel
{
  /**
   * Panel
   */
  GridLayout layout = new GridLayout( 2, 1, 5, 5 );
  JLabel jLblTitle = new JLabel();
  JTextField jTextCode = new JTextField();

  /**
   * Attributes
   */


  Product product;
  GUIOperation myContainer;

  /**
   * Constructs a new instance.
   */
  public GUIBarCode( GUIOperation pContainer )
  {
    super();
    try
    {
      myContainer = pContainer;
      jbInit();
    }
    catch( Exception e )
    {
      McdError.add( e );
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception
  {
    this.setBackground( Colors.BCE_BACK );
    this.setLayout( layout );
    this.setBorder( BorderFactory.createEmptyBorder( 0, 0, 10, 0 ) );
    jLblTitle.setText( Texts.BCE_TITLE );
    jLblTitle.setForeground( Colors.BCE_TEXT );
    jLblTitle.setHorizontalAlignment( SwingConstants.CENTER );
    this.add( jLblTitle, null );
    this.add( jTextCode, null );
    jTextCode.setEditable( true );
    jTextCode.addActionListener( new GUIBarCode_jTextCode_actionAdapter( this ) );
   
  }

  void jTextCode_actionPerformed( ActionEvent e )
  {
    String ls_code = this.jTextCode.getText();

    try
    {
      if ( !myContainer.externalSelection( ls_code ) )
        Toolkit.getDefaultToolkit().beep();

      this.jTextCode.setText( "" );
    }
    catch ( Exception ex )
    {
      McdError.add( ex );
    }
  }

  /**
   * Gives focus to text field
   */
  public void setFocus()
  {
    this.jTextCode.requestFocus();
  }

  /**
   * Sets the state of barcode reader
   * @param pState true to set it active, false to set it inactive
   */
  public void setActive( boolean pState )
  {
    this.jTextCode.setEditable( pState );
  }
}

class GUIBarCode_jTextCode_actionAdapter implements java.awt.event.ActionListener
{
  GUIBarCode adaptee;

  GUIBarCode_jTextCode_actionAdapter( GUIBarCode adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jTextCode_actionPerformed( e );
  }
}

